package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;

import com.baoyz.widget.PullRefreshLayout;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.CommandV2;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.adapter.delegator.RecyclerViewDelegatorV2;
import com.cn.aam.checaiduo.bean.me.PointDetailB;
import com.cn.aam.checaiduo.ui.me.adapter.PointDetailAdapter;
import com.cn.aam.checaiduo.ui.me.command.PointDetailCommand;
import com.cn.aam.checaiduo.utils.CalendarUtil;
import com.cn.aam.checaiduo.widget.MultiStateView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import butterknife.BindView;

/**
 * monker on 2017/6/20.
 * 积分明细
 */

public class PointDetailActivity extends BaseActivity implements OnDateSetListener, RecyclerViewDelegatorV2.OnLoadFirstPageLister<PointDetailB> {

    @BindView(R.id.id_recyclerview)
    RecyclerView mRecycler;
    @BindView(R.id.id_swipe)
    PullRefreshLayout mSwipe;
    @BindView(R.id.id_multistateview)
    MultiStateView idMultistateview;

    private PointDetailCommand command;
    private RecyclerViewDelegatorV2<PointDetailB> pointDelegator;
    private TimePickerDialog mDialogAll;
    private PointDetailAdapter adapter;
    private ToolBarManager toolbar;

    @Override
    protected int layoutId() {
        return R.layout.activity_point_detail;
    }


    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, PointDetailActivity.class);
        return intent;
    }

    @Override
    protected void initView() {
        toolbar = ToolBarManager.with(this, WMContent).setTitle("积分明细")
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setTitleColor(R.color.textColor)
                .setMenuTextContent("筛选")
                .setMenuTextColor(R.color.textColor)
                .setMenuTextClick(new ToolBarManager.OnMenuTextClickListener() {
                    @Override
                    public void onMenuTextClick() {
                        mDialogAll.show(getSupportFragmentManager(), "all");
                    }
                });

        initDate();
        command = new PointDetailCommand();
        adapter = new PointDetailAdapter(mContext);
        pointDelegator = new RecyclerViewDelegatorV2<>(this, idMultistateview, mSwipe, mRecycler, adapter, command);
        pointDelegator.setOnLoadFirstPageLister(this);
        pointDelegator.setPageSize(12);
        pointDelegator.beginHeaderRefreshing();
    }


    private void initDate() {
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setMaxMillseconds(System.currentTimeMillis())
                .setThemeColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                .setType(Type.YEAR)
                .setWheelItemTextNormalColor(ContextCompat.getColor(mContext, R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                .setWheelItemTextSize(17)
                .build();
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        String time = CalendarUtil.getYear(millseconds);
        toolbar.setMenuTextContent(time);
        timePickerView.dismiss();
        command.setData(time, "", "");
        pointDelegator.setDefault(true);
        adapter.setDataToAdapter(null);
        pointDelegator.beginHeaderRefreshing();
    }


    @Override
    public void onLoad(CommandV2<PointDetailB> mCommand) {
        ((PointDetailCommand) mCommand).setData("", "", "");
        toolbar.setMenuTextContent("筛选");
    }
}
